package com.sketch.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sketch.commom.PageResult;
import com.sketch.commom.Result;
import com.sketch.entity.AdvertiseConfig;
import com.sketch.entity.VipPrice;
import com.sketch.entity.dto.AdvertiseConfigDTO;
import com.sketch.entity.dto.VipPriceDTO;
import com.sketch.mapper.AdvertiseConfigMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class AdvertiseConfigService {
    @Resource
    private AdvertiseConfigMapper advertiseConfigMapper;

    public Result addOrUpdate(AdvertiseConfigDTO advertiseConfigDTO){
        AdvertiseConfig advertiseConfig = new AdvertiseConfig();
        BeanUtils.copyProperties(advertiseConfigDTO,advertiseConfig);
        if(ObjectUtils.isEmpty(advertiseConfigDTO.getId())){
            advertiseConfigMapper.insert(advertiseConfig);
        }else {
            advertiseConfigMapper.updateById(advertiseConfig);
        }

        return Result.success();
    }
    public Result list( Integer page, Integer size){
        Page<AdvertiseConfig> advertiseConfigPage = new Page<>(page, size);
        IPage<AdvertiseConfig> result = advertiseConfigMapper.selectPage(advertiseConfigPage,null);
        PageResult dtoPage = new PageResult<>();
        BeanUtils.copyProperties(result,dtoPage);
        List<AdvertiseConfigDTO> list = new ArrayList<>();
        if(ObjectUtils.isEmpty(result.getRecords())){
            return Result.success(dtoPage);
        }
        result.getRecords().forEach(e->{
            AdvertiseConfigDTO advertiseConfigDTO = new AdvertiseConfigDTO();
            BeanUtils.copyProperties(e,advertiseConfigDTO);
            list.add(advertiseConfigDTO);
        });
        dtoPage.setList(list);
        return Result.success().withData(dtoPage);
    }

    public Result detail(Long id){
        AdvertiseConfig advertiseConfig = advertiseConfigMapper.selectById(id);
        AdvertiseConfigDTO advertiseConfigDTO = new AdvertiseConfigDTO();
        BeanUtils.copyProperties(advertiseConfig,advertiseConfigDTO);
        return Result.success().withData(advertiseConfigDTO);
    }

    public Result delete(Long id){
        advertiseConfigMapper.deleteById(id);
        return Result.success();
    }
}
